package com.chinazyjr.framework.web.utils;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {
	
	private static final List<String> allowUrlArr = Arrays.asList(new String[] {
			"chinazyjr.com",
			"chinazyjr.cn"
	});
	
	private static final List<String> allowIpArr = Arrays.asList(new String[] {
			"192.168.7.223",
			"192.168.6.59",
			"192.168.7.220",
			"192.168.10.153"
	});
	
	public static boolean checkReferer(String refer) {
		if (refer.startsWith("http") && checkUrl(refer)) {
			return true;
		}
		if (checkIp(refer)) {
			return true;
		}
		
		return false;
	}

	// http://bbs.csdn.net/topics/190164265 - >  csdn.net
	public static boolean checkUrl(String url) {
		Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(url);
        // 二级域名
        if (matcher.find()) {
        	String group = matcher.group();
        	if (allowUrlArr.contains(group)) {
        		return true;
			}
		}
		return false;
	}

	public static boolean checkIp(String ip) {
		
		String regex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
		Pattern p = Pattern.compile(regex);
		Matcher matcher = p.matcher(ip);
		// ip地址
		if (matcher.find()) {
			 String group = matcher.group();
	        	if (allowIpArr.contains(group)) {
	        		return true;
				}
		}
		 
		return false;
	}

	
}
